<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <!-- <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="科室名称">
                <a-input v-model="queryParam.name" placeholder="" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => this.queryParam = {}">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div> -->

      <div class="table-operator">
        <a-button type="primary" icon="plus" @click="handleAdd">新建</a-button>
        <a-button type="primary" @click="handleConfig">配置</a-button>

        <a-dropdown v-action:edit v-if="selectedRowKeys.length > 0">
          <a-menu slot="overlay">
            <a-menu-item key="1"><a-icon type="delete" />删除</a-menu-item>
            <!-- lock | unlock -->
            <a-menu-item key="2"><a-icon type="lock" />锁定</a-menu-item>
          </a-menu>
          <a-button style="margin-left: 8px">
            批量操作 <a-icon type="down" />
          </a-button>
        </a-dropdown>
      </div>

      <a-table ref="table" size="default" rowKey="key" :columns="columns" :dataSource="loadData" :alert="false"
        :pagination="pagination"  @change="handleTableChange">
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="status" slot-scope="text">
          <a-badge :status="text | statusTypeFilter" :text="text | statusFilter" />
        </span>
        <span slot="description" slot-scope="text">
          <ellipsis :length="4" tooltip>{{ text }}</ellipsis>
        </span>

        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="showDrawer(record)">查看</a>
            <a-divider type="vertical" />
            <a @click="handleEdit(record)">编辑</a>
            <a-divider type="vertical" />
            <a @click="handleDelete(record)">删除</a>
          </template>
        </span>
      </a-table>

      <create-form ref="createModal" :visible="visible" :loading="confirmLoading" :model="mdl" @cancel="handleCancel"
        @ok="handleOk" />

      <config ref="config"></config>

      <a-drawer width="500" placement="right" :closable="false" :visible="showvisible" @close="onClose">
        <a-table bordered :data-source="dataSource" :columns="showcolumns">
          <template slot="getnum" slot-scope="text, record">
            <editable-cell :text="text" @change="onCellChange(record.key, 'getnum', $event)" />
          </template>
          <template slot="operation" slot-scope="text, record">
            <a-popconfirm v-if="dataSource.length" title="确定删除么?" @confirm="() => onDelete(record.key)">
              <a href="javascript:;">删除</a>
            </a-popconfirm>
          </template>
        </a-table>
      </a-drawer>

    </a-card>
  </page-header-wrapper>
</template>
<script>
import moment from 'moment'
import { STable, Ellipsis } from '@/components'
import { getRoleList, getServiceList } from '@/api/manage'
import CreateForm from './modules/CreateForm'
import Config from './modules/Config.vue'
import { Modal } from 'ant-design-vue';
import EditableCell from './modules/EditableCell'
import api from '@/api/yuyue/yueyue'

const columns = [
  {
    title: 'ID',
    dataIndex: 'id'
  },

  {
    title: '体检日期',
    dataIndex: 'rq',
  },
  {
    title: '每天数量',
    dataIndex: 'personCount',
  },
  {
    title: '开始时间',
    dataIndex: 'startHour',
  },
  {
    title: '结束时间',
    dataIndex: 'endHour',
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '150px',
    scopedSlots: { customRender: 'action' }
  }
]

const statusMap = {
  1: {
    status: 'success',
    text: '启用'
  },
  0: {
    status: 'error',
    text: '禁用'
  }
}

export default {
  name: 'TableList',
  components: {
    STable,
    Ellipsis,
    CreateForm,
    EditableCell,
    Config
  },
  data() {
    this.columns = columns
    return {
      // create model
      value: this.text,
      editable: false,
      visible: false,
      showvisible: false,
      confirmLoading: false,
      mdl: null,
      // 查询参数
      queryParam: {
        rq: null,
        pageNo: 0,
        pageSize: 2
      },
      pagination: {
        current: 1,
        pageSize: 2,
        total: 0,
        showTotal: total => `共 ${total} 条数据`,
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: [
        { id: 1, name: '2025-05', daynum: '20', remark: '这个是备注', updatedAt: '2020-10-10 10:10:10', status: 1 },
        { id: 2, name: '2025-06', daynum: '30', remark: '这个是备注', updatedAt: '2020-10-10 10:10:10', status: 1 },
      ],
      selectedRowKeys: [],
      selectedRows: [],
      dataSource: [
        {
          key: '0',
          getday: '2025-05-01',
          getnum: '11',
        },
        {
          key: '1',
          getday: '2025-05-02',
          getnum: '32',
        },
      ],
      count: 2,
      showcolumns: [
        {
          title: '日期',
          dataIndex: 'getday',
          width: '50%',
        },
        {
          title: '数量',
          dataIndex: 'getnum',
          scopedSlots: { customRender: 'getnum' },
        },
        {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operation' },
        },
      ],
      loading: false,
    }
  },
  filters: {
    statusFilter(type) {
      return statusMap[type].text
    },
    statusTypeFilter(type) {
      return statusMap[type].status
    }
  },
  created() {
    this.getList()
  },
  computed: {
    rowSelection() {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange
      }
    }
  },
  methods: {
    getList() {
      this.loading = true
      api.getList(this.queryParam).then(res => {
        this.loading = false
        this.loadData = res.data.dataList
        this.pagination.total = res.data.totalCount
        this.pagination.current = this.queryParam.pageNo + 1
      }).catch(err => {
        console.log(err);
        this.loading = false
      })
    },
    handleConfig() {
      this.$refs.config.show()
    },
    handleTableChange(e) {
      this.queryParam.pageNo = e.current - 1
      this.getList()
    },  
    handleChange(e) {
      const value = e.target.value;
      this.value = value;
    },
    check() {
      this.editable = false;
      this.$emit('change', this.value);
    },
    edit() {
      this.editable = true;
    },
    showDrawer() {
      this.showvisible = true;
    },
    onClose() {
      this.showvisible = false;
    },
    onCellChange(key, dataIndex, value) {
      const dataSource = [...this.dataSource];
      const target = dataSource.find(item => item.key === key);
      if (target) {
        target[dataIndex] = value;
        this.dataSource = dataSource;
      }
    },
    onDelete(key) {
      const dataSource = [...this.dataSource];
      this.dataSource = dataSource.filter(item => item.key !== key);
    },
    handleAdd() {
      this.mdl = null
      this.visible = true
    },
    handleEdit(record) {
      this.visible = true
      this.mdl = { ...record }
    },
    handleOk() {
      const form = this.$refs.createModal.form
      this.confirmLoading = true
      form.validateFields((errors, values) => {
        if (!errors) {
          console.log('values', values)
          if (values.id > 0) {
            // 修改 e.g.
            new Promise((resolve, reject) => {
              setTimeout(() => {
                resolve()
              }, 1000)
            }).then(res => {
              this.visible = false
              this.confirmLoading = false
              // 重置表单数据
              form.resetFields()
              // 刷新表格
              this.$refs.table.refresh()

              this.$message.info('修改成功')
            })
          } else {
            // 新增
            new Promise((resolve, reject) => {
              setTimeout(() => {
                resolve()
              }, 1000)
            }).then(res => {
              this.visible = false
              this.confirmLoading = false
              // 重置表单数据
              form.resetFields()
              // 刷新表格
              this.$refs.table.refresh()

              this.$message.info('新增成功')
            })
          }
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.visible = false

      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
    handleDelete(record) {
      // this.$message.info(`${record.id} 删除成功`)
      Modal.confirm({
        title: '提示',
        content: '确认删除？',
        okText: '确认',
        cancelText: '取消',
        onOk() {
          console.log('成功');

        }
      });
    },
    resetSearchForm() {
      this.queryParam = {
        date: moment(new Date())
      }
    }
  }
}

</script>

<style>
.editable-cell {
  position: relative;
}

.editable-cell-input-wrapper,
.editable-cell-text-wrapper {
  padding-right: 24px;
}

.editable-cell-text-wrapper {
  padding: 5px 24px 5px 5px;
}

.editable-cell-icon,
.editable-cell-icon-check {
  position: absolute;
  right: 0;
  width: 20px;
  cursor: pointer;
}

.editable-cell-icon {
  line-height: 18px;
  display: none;
}

.editable-cell-icon-check {
  line-height: 28px;
}

.editable-cell:hover .editable-cell-icon {
  display: inline-block;
}

.editable-cell-icon:hover,
.editable-cell-icon-check:hover {
  color: #108ee9;
}

.editable-add-btn {
  margin-bottom: 8px;
}
</style>